<template>
  <div class="building-container">
    <div class="table">
      <div class="search-container">
        <div class="search-label">楼宇名称：</div>
        <el-input
          v-model="data.name"
          clearable
          placeholder="请输入内容"
          class="search-main"
          @clear="doSearch"
        />
        <el-button type="primary" @click="findList">查询</el-button>
      </div>
      <hr>
      <!-- 添加楼宇弹框 -->
      <el-dialog
        :title="title"
        :visible="dialogVisible"
        width="580px"
        @close="closeDialog"
      >
        <div class="form-container">
          <el-form ref="addForm" :model="addForm" :rules="addFormRules">
            <el-form-item label="楼宇名称" prop="name">
              <el-input v-model="addForm.name" />
            </el-form-item>
            <el-form-item label="楼宇层数" prop="floors">
              <el-input v-model="addForm.floors" />
            </el-form-item>
            <el-form-item label="在管面积" prop="area">
              <el-input v-model="addForm.area" />
            </el-form-item>
            <el-form-item label="物业费" prop="propertyFeePrice">
              <el-input v-model="addForm.propertyFeePrice" />
            </el-form-item>
          </el-form>
        </div>
        <template #footer>
          <el-button
            size="mini"
            @click="dialogVisible = false"
          >取 消</el-button>
          <el-button size="mini" type="primary" @click="confirmAdd">确 定</el-button>
        </template>
      </el-dialog>
      <el-button type="primary" @click="addBuilding">添加楼宇</el-button>
      <el-table style="width: 100%" :data="buildingList">
        <el-table-column type="index" label="序号" />
        <el-table-column label="楼宇名称" width="180" prop="name" />
        <el-table-column label="层数" prop="floors" />
        <el-table-column label="在管面积(m²)" width="120" prop="area" />
        <el-table-column
          label="物业费(元/m²)"
          width="120"
          prop="propertyFeePrice"
        />
        <el-table-column label="状态" prop="status">
          <template>
            {{ buildingList.status ? "租赁中" : "空置中" }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="120">
          <template #default="{row}">
            <el-button size="mini" type="text" @click="edit(row)">编辑</el-button>
            <el-button size="mini" type="text" @click="confirmDel(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="page-container">
      <el-pagination
        layout="total, prev, pager, next"
        :total="total"
        @current-change="pageChange"
      />
    </div>
  </div>
</template>

<script>
import { getBuildList, addBuilding, delBuilding, upaateBuilding } from '@/apis/building'
export default {
  name: 'Mybuild',
  data() {
    return {
      data: {
        page: 1,
        pageSize: 10,
        name: ''
      },
      title: '',
      total: 0,
      buildingList: [],
      dialogVisible: false,
      addForm: {
        name: '',
        floors: null,
        area: null,
        propertyFeePrice: null
      },
      addFormRules: {
        name: [
          { required: true, message: '请输入楼宇名称', trigger: 'blur' }
        ],
        floors: [
          { required: true, message: '请输入楼宇层数', trigger: 'blur' }
        ],
        area: [
          { required: true, message: '请输入楼宇面积', trigger: 'blur' }
        ],
        propertyFeePrice: [
          { required: true, message: '请输入楼宇物业费', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    async getList() {
      const res = await getBuildList(this.data)
      // console.log(res)
      this.buildingList = res.data.rows
      this.total = res.data.total
    },
    pageChange(val) {
      this.data.page = val
      this.getList()
    },
    findList() {
      this.data.page = 1
      this.getList()
    },
    doSearch() {
      this.data.name = ''
      this.data.page = 1
      this.getList()
    },
    addBuilding() {
      this.dialogVisible = true
      this.title = '添加楼宇'
    },
    closeDialog() {
      this.dialogVisible = false
      this.$refs.addForm.resetFields()
    },
    // 添加楼宇
    confirmAdd() {
      this.$refs.addForm.validate(async(valid) => {
        if (valid) {
          if (this.title === '添加楼宇') {
            delete this.addForm.id
            await addBuilding(this.addForm)
            this.$message.success('添加成功')
          } else {
            await upaateBuilding(this.addForm)
            this.$message.success('编辑成功')
          }
          this.$refs.addForm.resetFields()
          this.dialogVisible = false
          this.getList()
        }
      })
    },
    async confirmDel(id) {
      this.$confirm('确认删除当前楼宇吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const { msg } = await delBuilding(id)
        this.getList()
        if (msg === '操作成功') {
          this.$message.success(msg)
        } else {
          this.$message.error(msg)
        }
      }).catch(() => {

      })
    },
    edit(row) {
      this.title = '编辑楼宇'
      this.dialogVisible = true
      const { id, area, floors, name, propertyFeePrice } = row
      this.addForm = {
        id, area, floors, name, propertyFeePrice
      }
    }
  }

}
</script>

<style lang="scss" scoped>
.search-container {
  display: flex;
  height: 32px;
  align-items: center;
  margin-bottom: 20px;
  .search-label {
    max-width: 100%;
    color: #000000d9;
    font-size: 14px;
  }
  .search-main {
    width: 220px;
    margin-right: 10px;
  }
}
.page-container {
  padding: 4px 0px;
}
</style>
